/*
 * @Date: 2023-03-14 13:09:41
 * @LastEditors: Chuying
 * @LastEditTime: 2023-04-03 14:19:31
 * @Description:
 */
/**
 * 应用程序的根模块
 */
import {
  Module,
  NestModule,
  MiddlewareConsumer,
  RequestMethod,
} from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { TypeOrmModule } from '@nestjs/typeorm';

import { HomeModule } from './modules/home/home.module';

import { UserModule } from './modules/account/user/user.module';
import { User } from './modules/account/user/entities/user.entity';

import { Posting } from './modules/business/posting/entities/posting.entity';
import { PostingModule } from './modules/business/posting/posting.module';
import { CategoryModule } from './modules/business/category/category.module';

import { Category } from './modules/business/category/entities/category.entity';
import { FileModule } from './modules/common/file/file.module';

import { AuthModule } from './modules/account/auth/auth.module';
import { UserInfo } from './modules/account/user/entities/user_info.entity';

import { JwtAuthGuard } from 'src/modules/account/auth/guards/jwt-auth.guard';

import { APP_FILTER, APP_GUARD } from '@nestjs/core';
import { ConfigModule, ConfigService } from '@nestjs/config';
import dbConfig from 'config/db.config';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      load: [dbConfig],
      envFilePath: [`.env.${process.env.NODE_ENV}`],
    }),
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: async (configService: ConfigService) => ({
        ...(await configService.get('database')),
      }),
      inject: [ConfigService],
    }),
    // TypeOrmModule.forRoot(), // 连接方式失败
    // TypeOrmModule.forRoot({
    //   type: 'mysql',
    //   port: 3306,
    //   username: 'root',
    //   //   host: 'localhost',
    //   //   password: 'admin',
    //   host: '124.220.15.206',
    //   password: 'hEjjygktt0>',
    //   database: 'nest',
    //   synchronize: true,
    //   entities: [User, Posting, Category, UserInfo], // 单独导入实体
    //   //   entities: [__dirname + '/../**/*.entity.{js,ts}'],
    //   //   autoLoadEntities: true, // 自动加载实体 比较推荐
    // }),
    HomeModule,
    UserModule,
    PostingModule,
    CategoryModule,
    FileModule,
    AuthModule,
  ], // 导入模块的列表
  controllers: [AppController], // 处理Http请求
  providers: [
    AppService, //  设置全局守卫，useClass为自定义的Guard
    {
      provide: APP_GUARD,
      useClass: JwtAuthGuard,
    },
  ], // 服务提供者
  //   exports:[] // 导出服务的列表
})
export class AppModule {}
